package com.cd2cd.code_template.configuration.security.loginUserService.impl;

import com.cd2cd.code_template.code_template.domain.SysAuth;
import com.cd2cd.code_template.code_template.domain.gen.SysAuthCriteria;
import com.cd2cd.code_template.code_template.mapper.SysAuthMapper;
import com.cd2cd.code_template.configuration.constants.DBConstants;
import com.cd2cd.code_template.configuration.security.TokenUser;
import com.cd2cd.code_template.configuration.security.loginUserService.LoadUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class SysAdminServiceImpl implements LoadUserService.SysAdmin {


    @Value("${system.login.password:-}")
    private String password;

    @Resource
    private SysAuthMapper sysAuthMapper;

    @Override
    public <T extends TokenUser> T loadUserByUsername(String username) {

        List<SysAuth> sysAuths = sysAuthMapper.selectByType(LoadUserService.SysAdmin.class.getSimpleName());

        Set<GrantedAuthority> authorities = new HashSet<>();

        sysAuths.forEach(item -> authorities.add(new SimpleGrantedAuthority(item.getGuid())));


        return (T) new TokenUser(null,username, password, LoadUserService.SysAdmin.class, authorities);
    }
}
